import walk from "./walk.js";
/*--------------------------模型点击事件------------------------------*/
export default function click(){
  // if(!isMobile)return 
  let time
  // 坐标转换
  const getMouse = (e) => {
    let mouse = new THREE.Vector3(); //二维面
    // 坐标转换
    if (isMobile) {
      // 手机端兼容
      if (e.changedTouches) {
        let touch = e.changedTouches[0];
        if (
          Math.abs(touch.pageX - e.touchStartX) +
            Math.abs(touch.pageX - e.touchStartY) >=
          3
        ) {
          return undefined;
        }
        mouse.x = (touch.pageX / window.innerWidth) * 2 - 1;
        mouse.y = -(touch.pageY / window.innerHeight) * 2 + 1;
      }else{
        mouse.x = (e.clientX / renderer.domElement.clientWidth) * 2 - 1;
        mouse.y = -((e.clientY / renderer.domElement.clientHeight) * 2) + 1;
      }
    } else {
      mouse.x = (e.clientX / renderer.domElement.clientWidth) * 2 - 1;
      mouse.y = -((e.clientY / renderer.domElement.clientHeight) * 2) + 1;
    }
    return mouse;
  };
  // 射线返回的模型集合
  const getOBj = (e) => {
    // e.preventDefault()
    let raycaster = new THREE.Raycaster(); //创建射线
    let mouse = getMouse(e);
    // 坐标转换
    raycaster.setFromCamera(mouse, camera);
    // intersectObjects返回所有被光线击中的模型
    let intersects = raycaster.intersectObjects(scene.children, true); //确定点击位置上的物体集合
    
    return intersects;
  };
  // 点击获取坐标
  const point=e=>{
    let arr = getOBj(e); //获取鼠标选中对象，e为点击对象
    let point=arr[0]
    target=point.point
    walk(point.point)
  }
  // 间隔超过100毫秒则执行移动
  const test=(e)=>{
    let delta=new Date-time
    if(delta<=200){
      point(e)
    }
  }
  
  window.addEventListener("pointerdown", ()=>time=new Date());
  window.addEventListener("pointerup", test);
}

